/*
 * DbIf.java
 *
 * Created on July 8, 2006, 1:26 PM
 *
 */

package org.vcboard.database;

import com.mysql.jdbc.CommunicationsException;
import java.awt.Component;
import java.sql.*;
import javax.swing.JOptionPane;
import org.apache.log4j.*;
import org.vcboard.*;
import org.vcboard.view.*;

/**
 * @author Josh
 */
public class DbIf
{
   static Logger log = Logger.getLogger(DbIf.class);
   
   Connection mysql = null;
   
   /** Creates a new instance of DbIf */
   public DbIf()
   {
   }
   
   public Connection regetMySQLConn()
   {
      mysql = null;
      
      try
      {
         mysql = getMySQLConn();
      }
      catch (CommunicationsException e)
      {
         log.error("Failed to reget MySQL connection");
      }
      
      return mysql;
   }
   
   public Connection getMySQLConn() throws CommunicationsException
   {
      // Create a connection to the database
      String serverName = WorkTimer.getProperties().getProperty("DB_HOST", "localhost");
      String username = WorkTimer.getProperties().getProperty("DB_USER", "root");
      String password = WorkTimer.getProperties().getProperty("DB_PASS", "password");
      String mydatabase = WorkTimer.getProperties().getProperty("DB_NAME", "work_timer");
      
      if (mysql == null)
      {
         try
         {
            String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
            Class.forName(driverName);
            
            String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
            mysql = DriverManager.getConnection(url, username, password);
         }
         catch (Exception e)
         {
            log.error("Failed to connect to MySQL: " + e);
            mysql = null;
         }
      }
      
      return mysql;
   }
   
   public Connection testMySQLConn(String host, String user, String pass, String name)
   {
      Connection test = null;
      
      try
      {
         String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
         Class.forName(driverName);
         
         log.info("Testing connection on " + host + " for " + name + " with " +
               user);// + " & " + pass);

         // Create a connection to the database
         String url = "jdbc:mysql://" + host + "/" + name;
         test = DriverManager.getConnection(url, user, pass);
      }
      catch (CommunicationsException e)
      {
         log.error("Failed to connect to MySQL server: " + e);

      }
      catch (Exception e)
      {
         log.error("Failed to connect to MySQL: " + e);
      }
      
      return test;
   }
   
   public void close(ResultSet rs)
   {
      try
      {
         rs.close();
      }
      catch (SQLException e)
      {
         log.error("Failed to close result set: " + e);
      }
   }
   
   public void close(Statement st)
   {
      try
      {
         st.close();
      }
      catch (SQLException e)
      {
         log.error("Failed to close statement: " + e);
      }
   }
   
   public void close(PreparedStatement ps)
   {
      try
      {
         ps.close();
      }
      catch (SQLException e)
      {
         log.error("Failed to close prepared statement: " + e);
      }
   }
}
